package com.example.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.example.constant.HttpConstant;

public class HttpUtil {
	public static String sendHttp(String servlet, String method,
			Map<String, String> map) {
		String result = null;
		StringBuffer sb = new StringBuffer();
		sb.append(HttpConstant.URL).append(servlet).append("?")
				.append("method=").append(method);
		if (map != null && map.size() != 0) {
			Iterator<Entry<String, String>> iterable = map.entrySet()
					.iterator();
			while (iterable.hasNext()) {
				Map.Entry<String, String> entry = iterable.next();
				Object key = entry.getKey();
				Object value = entry.getValue();
				sb.append("&").append(key).append("=").append(value);
			}
		}
		try {
			System.out.println(sb.toString());
			HttpGet httpGet = new HttpGet(sb.toString());
			HttpClient httpClient = new DefaultHttpClient();
			HttpResponse httpResponse = httpClient.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = EntityUtils.toString(httpResponse.getEntity());
				
			} else {
				result = null;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
}
